package com.sicau.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sicau.common.utils.SecurityUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sicau.common.annotation.Log;
import com.sicau.common.core.controller.BaseController;
import com.sicau.common.core.domain.AjaxResult;
import com.sicau.common.enums.BusinessType;
import com.sicau.system.domain.BMessage;
import com.sicau.system.service.IBMessageService;
import com.sicau.common.utils.poi.ExcelUtil;
import com.sicau.common.core.page.TableDataInfo;

/**
 * 消息Controller
 * 
 * @author ruoyi
 * @date 2022-03-19
 */
@RestController
@RequestMapping("/system/message")
public class BMessageController extends BaseController
{
    @Autowired
    private IBMessageService bMessageService;

    /**
     * 查询消息列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(BMessage bMessage)
    {
        startPage();
        bMessage.setUserId(SecurityUtils.getUserId());
        List<BMessage> list = bMessageService.selectBMessageList(bMessage);
        return getDataTable(list);
    }

    /**
     * 导出消息列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:export')")
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BMessage bMessage)
    {
        bMessage.setUserId(SecurityUtils.getUserId());
        List<BMessage> list = bMessageService.selectBMessageList(bMessage);
        ExcelUtil<BMessage> util = new ExcelUtil<BMessage>(BMessage.class);
        util.exportExcel(response, list, "消息数据");
    }

    /**
     * 获取消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bMessageService.selectBMessageById(id));
    }

    /**
     * 新增消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:add')")
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BMessage bMessage)
    {
        bMessage.setCreateBy(SecurityUtils.getUsername());
        return toAjax(bMessageService.insertBMessage(bMessage,bMessage.getUserIds()));
    }

    /**
     * 修改消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BMessage bMessage)
    {
        bMessage.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(bMessageService.updateBMessage(bMessage));
    }

    /**
     * 删除消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
    @Log(title = "消息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bMessageService.deleteBMessageByIds(ids));
    }
}
